import KmcLocalStorageFunction from './kmc-localStorageFunction'
/*============================================
TODO 加减计算，但进行浮点计算时还缺少四舍五入
============================================*/
module.exports = {
    /**
     * 加法运算
     */
    accAdd: function (arg1, arg2) {
        var r1, r2, m;
        try {
            r1 = arg1.toString().split(".")[1].length;
        }
        catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        }
        catch (e) {
            r2 = 0;
        }
        var m = Math.pow(10, Math.max(r1, r2));
        var n = (r1 >= r2) ? r1 : r2;
        return ((arg1 * m + arg2 * m) / m).toFixed(n);
    },

    /**
     * 减法运算
     * arg1: 被减数     arg2: 减数
     */
    subtr: function (arg1, arg2) {
        var r1, r2, m, n;
        try {
            r1 = arg1.toString().split(".")[1].length;
        }
        catch (e) {
            r1 = 0;
        }
        try {
            r2 = arg2.toString().split(".")[1].length;
        }
        catch (e) {
            r2 = 0;
        }
        var m = Math.pow(10, Math.max(r1, r2));
        //last modify by deeka
        //动态控制精度长度
        var n = (r1 >= r2) ? r1 : r2;
        return ((arg1 * m - arg2 * m) / m).toFixed(n);
    },

    /**
     * 乘法运算
     */
    mul: function(a, b) {
        var c = 0,
            d = a.toString(),
            e = b.toString();
        try {
            c += d.split(".")[1].length;
        }
        catch (f) {

        }
        try {
            c += e.split(".")[1].length;

        }
        catch (f) {

        }

        return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
    },

    /**
     * 除法运算
     * a: 被除数    b: 除数
     */
    divi: function (a, b) {
        var t1 = 0, t2 = 0, r1, r2;

        try {
            t1 = a.toString().split(".")[1].length;
        }
        catch (e) {

        }

        try {
            t2 = b.toString().split(".")[1].length;
        }
        catch (e) {

        }

        r1 = Number(a.toString().replace(".", ""));
        r2 = Number(b.toString().replace(".", ""));
        var reslut = (r1 / r2) * Math.pow(10, t2 - t1);
        if (KmcLocalStorageFunction.getLocalData("page") == "还款") { //买券还款-》1/3 1/2仓位 要是100 的整数倍
            reslut = Math.floor((reslut / r2) / 100) * 100;
        }
        return reslut;
    },

    /**
     * 自增运算
     * num: 初始值   unit: 最小自增单位      fixed: 保留小数点位数
     */
    Inc: function (num, unit, fixed) {
        return parseFloat(add(parseFloat(num), unit)).toFixed(fixed);
    },

    /**
     * 自减运算
     * num: 初始值   unit: 最小自增单位      fixed: 保留小数点位数
     */
    Dec: function (num, unit, fixed) {
        return parseFloat(sub(parseFloat(num), unit)).toFixed(fixed);
    },

    /**
     * 数字型字符串千分位转换
     * num: 初始值   fixedPoint: 截取小数点的位数
     */
    formatNumber: function(num, fixedPoint) {

        if (num == undefined || isNaN(num) || num == null || num == "") {
            return "";
        }

        fixedPoint = fixedPoint == undefined || fixedPoint == null ? "" : fixedPoint;

        num = (typeof(num) == 'string') ? parseFloat(num) : num;

        if (fixedPoint != "") {

            return (num.toFixed(fixedPoint) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
        } else {

            num += "";

            var point = num.indexOf(".");
            var len = num.length;

            //        数据为整型数          数据为浮点数
            var back = (point == -1) ? "" : num.slice(point - len);

            // 判断符号位
            var sign = num.slice(0, 1);

            point = (point == -1) ? len : point;

            // 为负数,从第二位数字进行千分位转换
            var front = (sign == "-") ? num.slice(1, point) : num.slice(0, point);

            sign = (sign == "-") ? sign : "";

            return sign + (front + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,') + back;
        }

    },
};
